# -*- coding: utf-8 -*-
import hashlib

from actions import *
from operations.weixin_operation import WeixinOperation

weixinAction = Blueprint("weixin_action",__name__)

wxOperation = WeixinOperation()

@weixinAction.route("/",methods=[HTTP_GET])
def weixin_get():
    signature = request.args.get("signature")
    timestamp = request.args.get("timestamp")
    nonce = request.args.get("nonce")
    echostr = request.args.get("echostr")
    #token = "etlinking"
    token = "zjkpython"
    tmpList = [token,timestamp,nonce]
    tmpList.sort()
    tmpStr = "".join(tmpList)
    hashStr = hashlib.sha1(tmpStr).hexdigest()
    if hashStr == signature:
        return echostr #success
    return "access verification fail" #fail

@weixinAction.route("/",methods=[HTTP_POST])
def weixin_post():
    xmlData = request.data
    result = wxOperation.saveData(xmlData)
    return result

@weixinAction.route("/xml",methods=[HTTP_GET])
def weixin_xml():
    xmlStr = """<xml>
                    <ToUserName><![CDATA[toUser]]></ToUserName>
                    <FromUserName><![CDATA[fromUser]]></FromUserName>
                    <CreateTime>123456789</CreateTime>
                    <MsgType><![CDATA[event]]></MsgType>
                    <Event><![CDATA[LOCATION]]></Event>
                    <Latitude>23.137466</Latitude>
                    <Longitude>113.352425</Longitude>
                    <Precision>119.385040</Precision>
                </xml>"""

    xmlData = """<xml>
                    <ToUserName><![CDATA[toUser]]></ToUserName>
                    <FromUserName><![CDATA[fromUser]]></FromUserName>
                    <CreateTime>1351776360</CreateTime>
                    <MsgType><![CDATA[location]]></MsgType>
                    <Location_X>23.134521</Location_X>
                    <Location_Y>113.358803</Location_Y>
                    <Scale>20</Scale>
                    <Label><![CDATA[位置信息]]></Label>
                    <MsgId>1234567890123456</MsgId>
                </xml>"""
    #xmlRoot = ElementTree.fromstring(xmlStr1)
    #eventNode = xmlRoot.find("Event")
    #return "%s %s" % (eventNode.tag,eventNode.text)
    #return xmlStr
    result = wxOperation.saveData(xmlData)
    return result

def get_response_date():

    pass

